package com.huarui.inventory.dao.impl;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.ewaf.framework.dao.impl.BaseDaoImpl;
import org.springframework.orm.ibatis.SqlMapClientCallback;

import com.huarui.inventory.dao.IInventoryDao;
import com.huarui.inventory.model.Inventory;
import com.huarui.inventory.model.InventoryListModel;
import com.huarui.quotation.model.QuotationListModel;
import com.ibatis.sqlmap.client.SqlMapExecutor;
import com.sun.tools.extcheck.ExtCheck;

public class InventoryDaoImpl extends BaseDaoImpl implements IInventoryDao {

	@Override
	public Inventory getInventoryByIdAndCompanyId(Integer id, Integer companyId,boolean update) {

		Map<String, Object> map = new HashMap<String, Object>();

		map.put("id", id);
		map.put("companyId", companyId);
		map.put("update", update);
		return (Inventory) getSqlMapClientTemplate().queryForObject(
				namespace + ".getByIdAndCompanyId", map);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Map<String,Object>>queryInventoryByCondition(
			InventoryListModel inventoryListModel) {

		return getSqlMapClientTemplate().queryForList(namespace + ".querys",
				inventoryListModel);
	}

	public int countByConditions(InventoryListModel inventoryListModel) {
		return (Integer) getSqlMapClientTemplate().queryForObject(
				namespace + ".countByConditions", inventoryListModel);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Inventory> getInventoryByConditions(Map map) {

		return (List<Inventory>) getSqlMapClientTemplate().queryForList(
				namespace + ".getInventoryByConditions", map);

	}

	/**
	 * 批量更新库存剩余数
	 */
	@Override
	public void batchUpdateInventory(final List<Inventory> inventories) {

		getSqlMapClientTemplate().execute(new SqlMapClientCallback() {
			@Override
			public Object doInSqlMapClient(SqlMapExecutor executor)
					throws SQLException {
				executor.startBatch();
				for (Inventory inventory : inventories) {
					executor.update(namespace + ".update", inventory);
				}
				executor.executeBatch();
				return null;
			}
		});

	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Map<String, Object>> queryForListBySize(
			Map<String, Object> map, int size) {

		return getSqlMapClientTemplate().queryForList(
				namespace + ".queryBySize", map, 0, size);
	}

	public void batchInsertInventory(final List<Inventory> inventories) {
		getSqlMapClientTemplate().execute(new SqlMapClientCallback() {

			@Override
			public Object doInSqlMapClient(SqlMapExecutor executor)
					throws SQLException {
				executor.startBatch();
				int batch = 0;
				for (Inventory inventory : inventories) {
					executor.insert(namespace + ".create", inventory);
					batch++;
					if (batch > 500) {
						executor.executeBatch();
						batch = 0;
					}
				}
				executor.executeBatch();
				for(Inventory obj:inventories){
					System.out.println(obj.getId());
				}
				return null;
			}
		});

	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Inventory> queryInventoryByCondition(
			QuotationListModel quotationListModel) {

		return getSqlMapClientTemplate().queryForList(
				namespace + ".queryInventory", quotationListModel);
	}


	@Override
	public void batchUpdateInventoryList(final List<Inventory> inventoryList) {
		getSqlMapClientTemplate().execute(new SqlMapClientCallback() {
			@Override
			public Object doInSqlMapClient(SqlMapExecutor executor)
					throws SQLException {
				executor.startBatch();
				for (Inventory inventory : inventoryList) {
					executor.update(namespace + ".update_another", inventory);
				}
				executor.executeBatch();
				return null;
			}
		});
	}
	
	@Override
	public Integer showInventoryToSaleCount(Map<String, Object> paramsMap) {
		return (Integer) this.getSqlMapClientTemplate().queryForObject(super.namespace + ".showInventoryToSaleCount", paramsMap);
	}
	
	@Override
	@SuppressWarnings("unchecked")
	public List<Map<String, Object>> showInventoryToSale(Map<String, Object> paramsMap) {
		return this.getSqlMapClientTemplate().queryForList(super.namespace + ".showInventoryToSaleOnPage", paramsMap);
	}

	@Override
	public Integer getInventoryCountByCondition(Map<String, Object> quotaMap) {		
		return (Integer) getSqlMapClientTemplate().queryForObject(namespace+".getInventoryCountByCondition", quotaMap);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Map> getInventoryForMapByCondition(Map<String, Object> quotaMap) {	
		return getSqlMapClientTemplate().queryForList(namespace+".getInventoryForMapByCondition", quotaMap);
	}
	
	@SuppressWarnings("unchecked")
	public List<Inventory> getListByCondition(Map<String, Object> map){
		return getSqlMapClientTemplate().queryForList(namespace+".getListByCondition",map);
	}
	
	public Integer getCountByCondition(Map<String, Object> map){
		return (Integer) getSqlMapClientTemplate().queryForObject(namespace+".getCountByCondition",map);
	}

	@Override
	public Integer getCountByConditions(Map<String, Object> queryMap) {
		
		return (Integer) getSqlMapClientTemplate().queryForObject(namespace+".getCountByConditions", queryMap);
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<Map<String, Object>> getMapByConditions(Map<String, Object> queryMap) {
		return getSqlMapClientTemplate().queryForList(namespace+".getMapByConditions", queryMap);
	}
}
